package com.zzh.wsss.service.impl;

import com.zzh.wsss.mapper.ApplyOperateMapper;
import com.zzh.wsss.mapper.UserOperateMapper;
import com.zzh.wsss.pojo.Apply;
import com.zzh.wsss.pojo.User;
import com.zzh.wsss.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserLoginServiceImpl implements UserLoginService {

    @Autowired
    private UserOperateMapper userOperateMapper;

    @Autowired
    private ApplyOperateMapper applyOperateMapper;

    public Boolean userlogin(String username, String password) {
        List<User> list=userOperateMapper.getPassword(username);
        if(list.size()==0)return false;
        User user=list.get(0);
        if(user.getPassword().equals(password)){
            return true;
        }
        else return false;
    }

    public User userloginmsg(String username) {
        return userOperateMapper.getUserMag(username);
    }


    public Map<String, Object> getApplyResult(String username) {
        Map<String,Object> map=new HashMap<String, Object>();
        List<Apply> list=applyOperateMapper.getApplyResult(userOperateMapper.getUserId(username).getId());
        if(list.size()!=0){
            Apply apply=list.get(0);
            if(apply.getResults()!=null){
                if(apply.getResults()==0){
                    applyOperateMapper.deleteApply(apply.getId());
                    map.put("result",0);
                }else{
                    if(apply.getAction()==1||apply.getAction()==2){
                        userOperateMapper.updataUserJob(username,apply.getJobId());
                    }else{
                        userOperateMapper.updataUserJobNull(username);
                    }
                    applyOperateMapper.deleteApply(apply.getId());
                    map.put("result",1);
                }
            }
        }
        return map;
    }
}
